package com.softer.wxzj.common.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.softer.wxzj.common.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2019/12/5
 */
@Configuration
public class WebConfigurer implements WebMvcConfigurer {
    /**
     * 登陆校验的拦截器注入
     */
    @Autowired
    private MyInterceptor myInterceptor;
    /**
     * 需要拦截的路径
     */
    String[] addPathPatterns = {
            //"/**"
    };
    /**
     * 不需要拦截的路径
     */
    String[] excludePathPatterns = {
            "/static/js/**",
            "/static/css/**",
            "/static/img/**",
            "/login",
            "/logout",
            "/excel/**",
            "/file/upload",
            "/Captcha.jpg",
            "/sysLoginInfo/addLoginInfo",
            "/baseOrganization/submitAuditAndAdd",
            "/file/saveFile",
            "/file/delete",
            "/baseOwnerCommittee/getCommitteeRegionList",
            "/baseOrganization/selectByPassWord",
            "/file/getFilePathListByFileUpload",
            "/baseOrganization/getModifyListToExtraNet",
            "/baseOwnerCommittee/getModifyListToExtraNet",
            "/diccontent/selectForInput",
            "/verificationCode",
            "/baseOrganization/resetPassword",
            "/baseOrganization/rejectionExaminationApproval",
            "/baseOwnerCommittee/rejectionExaminationApproval",
            "/baseOrganization/updateAndAdd",
            "/baseOrganization/getInfo",
            "/baseOwnerCommittee/getInfo",
            "/baseOrganization/getModifyDetailList",
            "/file/getModifyFileDetail",
            "/file/getFileVOList",
            "/sysOper/selectOperNoByCompanyId",
            "/zoneCode/zoneCodeListWholeOut",
            "/home/getResourceTree",
            "/home/getMenu",
            "/home/getLoginInfo",
            "/baseOrganization/getExtraNetModify",
            "/sysOper/getOutUserListByCompanyId",
            "/sysOper/getRoleListByCompanyType",
            "/sysOper/insertOutUser",
            "/sysOper/getInfo",
            "/sysOper/updateOutOper",
            "/sysOper/bindUsbKey",
            "/sysOper/getOperInfoByOperNo",
            "/sysOper/getUsbKeyIsBind",
            "/sysOper/updatePassword",
            "/orgBank/add",
            "/orgBank/getOrgBankInfoListFromExtraNet",
            "/zoneCode/getAllZoneName",
            "/baseRegionInfo/getRegionName",
            "/orgBank/getReqListById",
            "/orgBank/updateState",
            "/baseOwnerCommittee/getModifyDetailList",
            "/orgBank/getInfo",
            "/orgBank/updateOrg",
            "/orgBank/getModifyDetailList",
            "/baseBankInfo/getAllList",
            "/orgRegion/addOrgRegion",
            "/orgRegion/deleteOrgRegion",
            "/orgRegion/getOrgRegionList",
            "/orgRegion//getAllRegion",
            "/orgRegion/getRegionList",
            "/baseSubRegionInfo/selectDown",
            "/orgBuild/getOrgBuildAuditList",
            "/orgBuild/getAllOrgBuildAuditList",
            "/orgBuild/getAddBuildList",
            "/orgBuild/deleteAudit",
            "/orgBuild/addAudit",
            "/orgBuild/submit",
            "/baseRegionInfo/getInfo",
            "/baseHouseInfo/selectCountByRegionNo",
            "/expProjectReq/preview",
            "/baseBuildInfo/getInfoBuild",
            "/baseHouseInfo/selectCountByBuildId",
            "/baseCellInfo/getAllList",
            "/baseHouseInfo/houseAndOwner",
            "/BaseBuildView/getView",
            "/BaseBuildView/getHouseCount",
            "/expProjectReq/getBuildProjectList",
            "/expProjectReq/getInfo",
            "/expProjectReq/getApportionInfo",
            "/expProjectReq/apportionDetail",
            "/expSurveyInfo/getAllList",
            "/ownerSignature/getInfo",
            "/ownerSignature/getSum",
            "/expPayReq/getListByReqId",
            "/expProjectReq/getFinishInfo",
            "/maintenanceEquipment/add",
            "/maintenanceEquipment/update",
            "/maintenanceEquipment/getEquipmentListByOperNo",
            "/maintenanceEquipment/enable",
            "/maintenanceEquipment/delete",
            "/maintenanceObject/getMaintenanceObjectList",
            "/maintenanceObject/getEffectiveMaintenanceObjectList",
            "/maintenanceEquipment/getInfo",
            "/syscoreparam/getParamByParamCode",
            "/flowAuditdetail/getCurrentAuditdetailList",
            "/expProjectReq/add",
            "/expProjectReq/update",
            "/baseOrganization/getSupervisionOrgList",
            "/baseOrganization/getAuditOrgList",
            "/baseOrganization/getConstructionOrgList",
            "/expProjectReq/preview",
            "/expProjectReq/delete",
            "/expRevocation/projectRevocation",
            "/expSurveyInfo/add",
            "/expSurveyInfo/update",
            "/expSurveyInfo/delete",
            "/expSurveyInfo/getInfo",
            "/ownerSignature/update",
            "/expProjectReq/updateFinish",
            "/expProjectReq/whetherAllowDeclaration",
            "/orgBuild/getOrgRegionAuditList",
            "/orgBuild/getOrgBuildInfoByOrgRegionId",
            "/expProjectReq/selectMaintenanceEquipment",
            "/expProjectReq/batchCommitFromExtraNet",
            "/expProjectReq/updateState",
            "/expProjectSearch/expGrantInfoSum",
            "/expRevocation/payRevocation",
            "/expPayReq/grantApply",
            "/expProjectReq/projectCheckList",
            "/expProjectReq/appropriationCheckList",
            "/expProjectReq/apportionInfoSave",
            "/expProjectReq/reset",
            "/expProjectReq/inputFinishNumMax",
            "/home/getAllRegionByOperNo",
            "/baseOrganization/getOrgSupervisionInfo",
            "/baseOrganization/getReminder",
            "/sysOper/openOrCloseAccount",
            "/baseOwnerCommittee/submitAuditAndAdd",
            "/baseOwnerCommittee/updateAndAdd",
            "/baseRegionInfo/selectCommitteByZoneCode",
            "/expProjectReq/apportionform",
            "/flowAuditdetail/getHistoryList",
            "/flowConfig/getConfigListByFlowType",
            "/flowConfig/getApplicant",
            "/diccontent/getRegisterType",
            "/flowConfig/getCurrentConfigByBusinessId",
            "/expProjectReq/finishCancel",
            "/baseOwnerCommittee/checkRegion",
            "/baseOwnerCommittee/getOrgRegionList",
            "/expProjectReq/getEmergencyAuditSearchList",
            "/expProjectReq/projectCheckListForCommittee",
            "/expProjectReq/appropriationCheckListForCommittee",
            "/expProjectReq/previewForCommittee",
            "/expProjectReq/audit",
            "/expPublic/add",
            "/expPublic/getInfo",
            "/expPayReq/selectByPayReqIdForm",
            "/netCaptcha.jpg",
            "/index.html"
    };

    /**
     * 这个方法用来注册拦截器，我们自己写好的拦截器需要通过这里添加注册才能生效
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addPathPatterns("/**") 表示拦截所有的请求，
        // excludePathPatterns("/login", "/register") 表示除了登陆与注册之外，因为登陆注册不需要登陆也可以访问
        registry.addInterceptor(myInterceptor).addPathPatterns(addPathPatterns).excludePathPatterns(excludePathPatterns);
    }
    @Bean
    public HttpMessageConverters customConverters(){
        // 1.需要定义一个convert转换消息的对象;
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        // 2:添加fastJson的配置信息;
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
        // 3处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        // 4.在convert中添加配置信息.
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;
        return new HttpMessageConverters(converter);
//        return new HttpMessageConverters(new FastJsonHttpMessageConverter());
    }
}
